package cn.aitstone.org.service.impl;

import cn.aitstone.basic.query.BaseQuery;
import cn.aitstone.basic.service.impl.BaseServiceImpl;
import cn.aitstone.basic.utils.MD5Utils;
import cn.aitstone.basic.utils.PageList;
import cn.aitstone.basic.utils.StrUtils;
import cn.aitstone.org.domain.Employee;
import cn.aitstone.org.mapper.EmployeeMapper;
import cn.aitstone.org.query.EmployeeQuery;
import cn.aitstone.org.service.IEmployeeService;
import cn.aitstone.user.domain.Logininfo;
import cn.aitstone.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description: service层实现类
 * @Author: Sheldon
 * @Version: 1.0
 * @Date:  2022/6/21 19:33
 */
@Service
// 只读事务
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    /**
     * @Title: loadAll
     * @Description: 查询所有
     * @Author: Sheldon
     * @Version: 1.0
     * @Date:  2022/6/21 19:34
     * @Parameters: []
     * @Return java.util.List<cn.aitstone.org.domain.T>
     */
    @Override
    public List<Employee> loadAll() {
        return employeeMapper.loadAll();
    }

    @Override
    public Employee loadById(Long id) {
        return employeeMapper.loadById(id);
    }

    @Override
    public void remove(Long id) {
        employeeMapper.remove(id);
        logininfoMapper.remove(id);
        employeeMapper.removeEmployeeRole(id);
    }

    public void initRelationTable(Employee employee){
        // 2.根据roleId和菜单Id和权限Id进行新增
        if (employee.getRoles() != null){
            // 1.根据roleId删除角色菜单和角色权限关联表对应数据
            employeeMapper.removeEmployeeRole(employee.getId());
            employeeMapper.saveEmployeeRole(employee.getId(), employee.getRoles());
        }
    }
    /**
    * @Title: save
    * @Description: 保存方法
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/6/21 19:35
    * @Parameters: [employee]
    * @Return void
    */
    @Override
    @Transactional
    public void save(Employee employee) {
        initEmployee(employee);
        Logininfo logininfo = employ2Logininfo(employee);
        logininfoMapper.save(logininfo);
        employee.setLogininfoId(logininfo.getId());
        employeeMapper.save(employee);
        if (employee.getRoles() != null){
            employeeMapper.saveEmployeeRole(employee.getId(), employee.getRoles());
        }
    }

    private Logininfo employ2Logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee, logininfo);
        logininfo.setType(0);
        return logininfo;
    }

    private void initEmployee(Employee employee) {
        String salt = StrUtils.getComplexRandomString(32);
        employee.setSalt(salt);
        String saltPassword = MD5Utils.encrypByMd5(salt + employee.getPassword());
        employee.setPassword(saltPassword);
    }

    /**
    * @Title: update
    * @Description: 修改
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/6/21 19:36
    * @Parameters: [employee]
    * @Return void
    */
    @Override
    @Transactional
    public void update(Employee employee) {
        //initEmployee(employee);
        employeeMapper.update(employee);
        Logininfo logininfo = employ2Logininfo(employee);
        logininfo.setId(employee.getLogininfoId());
        logininfoMapper.update(logininfo);
        initRelationTable(employee);

    }


    /**
    * @Title: queryData
    * @Description: 查询分页数据
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/6/21 19:36
    * @Parameters: [employeeQuery]
    * @Return cn.aitstone.basic.utils.PageList<cn.aitstone.org.domain.Employee>
    */
    @Override
    public PageList<Employee> queryData(EmployeeQuery employeeQuery) {
        List<Employee> rows = employeeMapper.queryData(employeeQuery);
        Integer total = employeeMapper.queryCount(employeeQuery);
        return new PageList<>(total, rows);

    }

    @Override
    public void batchdel(Long[] ids) {
        employeeMapper.batchdel(ids);
    }

    @Override
    public Employee loadByLogininfoId(Long id) {
        return employeeMapper.loadByLogininfoId(id);
    }

    @Override
    public List<Employee> loadByShopId(Long shopId) {
        return employeeMapper.loadByShopId(shopId);
    }
}
